package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Controller;

import java.io.IOException;

@Controller
@RabbitListener(queues = "java-queue")
public class RabbitAcceptController {

//    @RabbitListener(queues = "java-queue")可以标在类和方法上
    @RabbitHandler//只能标再方法上
    public void acceptMeg(Message message,OrderEntity orderEntity, Channel channel){
        System.out.println("这是OrderEntity:"+orderEntity);
        System.out.println("处理完成"+orderEntity.getId());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            if (orderEntity.getId()>5L){

                //deliveryTag为 消息队列中的id
                //false 是否 批处理
                System.out.println(deliveryTag);
                channel.basicAck(deliveryTag,false);
            }else {
                System.out.println(deliveryTag);
                channel.basicNack(deliveryTag,false,true);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @RabbitHandler
    public void acceptMeg(OrderItemEntity message){
        System.out.println("这是OrderItemEntity:"+message);
    }
}
